package com.jxin.oms.order.domain.inorder.converter;

import com.jxin.oms.order.domain.inorder.dmo.entity.InOrderDetail;
import com.jxin.oms.order.domain.inorder.dmo.vao.OtherValue;
import com.jxin.oms.order.domain.inorder.repository.table.InOrderDetailDo;
import com.jxin.oms.order.web.dto.req.InOrderDetailSaveReqDto;
import com.jxin.util.serialize.SerializeUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  Jxin
 * @version 1.0
 * @since   2020-01-21 14:49:12
 */
public class InOrderDetailConverter {
    private InOrderDetailConverter(){}
    /**
     * Convert InOrderDetailDo to InOrderDetail
     * @param inOrderDetailDo 原始单详情 数据对象 Do
     * @return {@link InOrderDetail}
     * @author Jxin
     */
    public static InOrderDetail convertToInOrderDetail(InOrderDetailDo inOrderDetailDo) {
        Assert.notNull(inOrderDetailDo, "inOrderDetailDo不能为null");
        
        final InOrderDetail result = new InOrderDetail();

        result.setInOrderId(inOrderDetailDo.getInOrderId());
        result.setInOrderDetailId(inOrderDetailDo.getInOrderDetailId());
        result.setDetailStatus(inOrderDetailDo.getDetailStatus());
        result.setReturnStatus(inOrderDetailDo.getReturnStatus());
        result.setActivity(inOrderDetailDo.getActivity());
        result.setGroupNum(inOrderDetailDo.getGroupNum());
        result.setErrType(inOrderDetailDo.getErrType());
        result.setErrMsg(inOrderDetailDo.getErrMsg());
        result.setSku(inOrderDetailDo.getSku());
        result.setAmount(inOrderDetailDo.getAmount());
        result.setPresellTime(inOrderDetailDo.getPresellTime());
        result.setSendTime(inOrderDetailDo.getSendTime());
        result.setReceivingTime(inOrderDetailDo.getReceivingTime());
        result.setModifyTime(inOrderDetailDo.getModifyTime());
        result.setCreateTime(inOrderDetailDo.getCreateTime());

        final byte[] otherValueBytes = inOrderDetailDo.getOtherValueBytes();
        if(ArrayUtils.isNotEmpty(otherValueBytes)){
            result.setOtherValue(SerializeUtil.deserialize(otherValueBytes, OtherValue.class));
        }

        result.setIsDeleted(inOrderDetailDo.getIsDeleted());
        return result;
    }
    /**
     * Convert InOrderDetailDo to InOrderDetail
     * @param inOrderDetailSaveReqDto 原始单详情落盘 传输对象
     * @return {@link InOrderDetail}
     * @author Jxin
     */
    public static InOrderDetail convertToInOrderDetail(InOrderDetailSaveReqDto inOrderDetailSaveReqDto) {
        Assert.notNull(inOrderDetailSaveReqDto, "inOrderDetailSaveReqDto不能为null");

        final InOrderDetail result = new InOrderDetail();

        result.setInOrderId(inOrderDetailSaveReqDto.getInOrderId());
        result.setInOrderDetailId(inOrderDetailSaveReqDto.getInOrderDetailId());
        result.setDetailStatus(inOrderDetailSaveReqDto.getDetailStatus());
        result.setReturnStatus(inOrderDetailSaveReqDto.getReturnStatus());
        result.setActivity(inOrderDetailSaveReqDto.getActivity());
        result.setGroupNum(inOrderDetailSaveReqDto.getGroupNum());
        result.setSku(inOrderDetailSaveReqDto.getSku());
        result.setAmount(inOrderDetailSaveReqDto.getAmount());
        result.setPresellTime(inOrderDetailSaveReqDto.getPresellTime());

        result.setOtherValue(OtherValue.newOfValueMap(inOrderDetailSaveReqDto.getOtherValue()));

        return result;
    }
    /**
     * Convert InOrderDetail to InOrderDetailDo
     * @param inOrderDetail 原始单详情 实体
     * @return {@link InOrderDetailDo}
     * @author Jxin     
     */
    public static InOrderDetailDo convertToInOrderDetailDo(InOrderDetail inOrderDetail) {
      Assert.notNull(inOrderDetail, "inOrderDetail不能为null");
      
      final InOrderDetailDo result = new InOrderDetailDo();

        result.setInOrderId(inOrderDetail.getInOrderId());
        result.setInOrderDetailId(inOrderDetail.getInOrderDetailId());
        result.setDetailStatus(inOrderDetail.getDetailStatus());
        result.setReturnStatus(inOrderDetail.getReturnStatus());
        result.setActivity(inOrderDetail.getActivity());
        result.setGroupNum(inOrderDetail.getGroupNum());
        result.setErrType(inOrderDetail.getErrType());
        result.setErrMsg(inOrderDetail.getErrMsg());
        result.setSku(inOrderDetail.getSku());
        result.setAmount(inOrderDetail.getAmount());
        result.setPresellTime(inOrderDetail.getPresellTime());
        result.setSendTime(inOrderDetail.getSendTime());
        result.setReceivingTime(inOrderDetail.getReceivingTime());
        result.setModifyTime(inOrderDetail.getModifyTime());
        result.setCreateTime(inOrderDetail.getCreateTime());

        result.setOtherValueBytes(SerializeUtil.serialize(inOrderDetail.getOtherValue()));

        result.setIsDeleted(inOrderDetail.getIsDeleted());
        return result;
    }
    /**
     * Convert InOrderDetailList to InOrderDetailDoList
     * @param inOrderDetailList 原始单详情 实体 列表
     * @return {@link InOrderDetailDo}
     * @author Jxin
     */
    public static List<InOrderDetailDo> convertToInOrderDetailDoList(List<InOrderDetail> inOrderDetailList) {
        Assert.notEmpty(inOrderDetailList, "inOrderDetailList不能为空");

        final List<InOrderDetailDo> result = new ArrayList<>(inOrderDetailList.size());
        for (InOrderDetail inOrderDetail : inOrderDetailList) {
            result.add(convertToInOrderDetailDo(inOrderDetail));
        }
        return result;
    }
}
